package com.example.yijian.edit.view;

import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.yijian.R;
import com.example.yijian.base.BaseConstants;
import com.example.yijian.edit.grallyRecyclerView.GrallyScaleHelper;
import com.example.yijian.edit.grallyRecyclerView.ItemTouchListener;
import com.example.yijian.edit.grallyRecyclerView.OnGrallyItemClickListener;
import com.example.yijian.edit.grallyRecyclerView.SpeedRecyclerView;
import com.example.yijian.utils.datainfo.ClipInfo;
import com.example.yijian.utils.datainfo.TimelineData;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class DragSortView extends RelativeLayout  {
    private View rootView;
    private Context mContext;
    private RecyclerView speedRecyclerView;
    private GrallyAdapter mGrallyAdapter;
    private GrallyScaleHelper mGrallyScaleHelper;
    private CallBack fcallBack;

    public DragSortView(Context context,CallBack callBack) {
        super(context);
        mContext = context;
        this.fcallBack=callBack;
        this.init();
    }

    public DragSortView(Context context, AttributeSet attrs,CallBack callBack) {
        super(context, attrs);
        mContext = context;
        this.fcallBack=callBack;
        this.init();
    }

    public DragSortView(Context context, AttributeSet attrs, int defStyleAttr,CallBack callBack) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        this.fcallBack=callBack;
        this.init();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public DragSortView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes,CallBack callBack) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mContext = context;
        this.fcallBack=callBack;
        this.init();
    }

    private void init() {
        rootView = LayoutInflater.from(mContext).inflate(R.layout.view_drag_sort, this);
        iniview();
    }

    private void iniview() {
        speedRecyclerView = findViewById(R.id.editClipRecyclerView);
    }

    public void setData(ArrayList<ClipInfo> list) {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
        speedRecyclerView.setLayoutManager(linearLayoutManager);
        mGrallyAdapter = new GrallyAdapter(mContext);
        mGrallyAdapter.setClipInfoArray(list);
        speedRecyclerView.setAdapter(mGrallyAdapter);

        ItemTouchHelper.Callback callback = new com.example.yijian.edit.grallyRecyclerView.ItemTouchHelper(mGrallyAdapter) {
            @Override
            public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                super.clearView(recyclerView, viewHolder);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        DragSortView.this.setVisibility(View.GONE);
                        TimelineData.instance().setClipInfoData(list);
                        fcallBack.finshCallBack();
                    }
                }, 500);
            }
        };
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(callback);
        itemTouchHelper.attachToRecyclerView(speedRecyclerView);
        mGrallyScaleHelper = new GrallyScaleHelper();
        mGrallyScaleHelper.attachToRecyclerView(speedRecyclerView);
    }


    public class GrallyAdapter extends RecyclerView.Adapter<GrallyAdapter.ViewHolder> implements ItemTouchListener {
        private Context mContext;
        private List<ClipInfo> mClipInfoArray = new ArrayList<>();
        private GrallyScaleHelper mCardAdapterHelper = new GrallyScaleHelper();
        private OnGrallyItemClickListener m_onItemClickListener = null;
        private int mSelectPos = 0;

        public GrallyAdapter(Context context) {
            this.mContext = context;
        }

        public void setClipInfoArray(List<ClipInfo> clipInfoArray) {
            this.mClipInfoArray = clipInfoArray;
        }

        public void setOnItemSelectedListener(OnGrallyItemClickListener m_onItemClickListener) {
            this.m_onItemClickListener = m_onItemClickListener;
        }

        public void setSelectPos(int pos) {
            mSelectPos = pos;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_recyclerview_grally, parent, false);
            return new ViewHolder(itemView);
        }

        @Override
        public void onBindViewHolder(final ViewHolder holder, int position) {
            setBindViewHolder(holder, position);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position, List<Object> payloads) {
            if (payloads.isEmpty()) {
                onBindViewHolder(holder, position);
            } else {
                setBindViewHolder(holder, position);
            }
        }

        private void setBindViewHolder(final ViewHolder holder, int position) {
            mCardAdapterHelper.onBindViewHolder(holder.itemView, position, getItemCount(), holder.mImageView);
            if (mSelectPos != position) {
                holder.itemView.setScaleY(BaseConstants.EDITGRALLYSCALE);
            } else {
                holder.itemView.setScaleY(1);
            }
            RequestOptions options = new RequestOptions();
            options.centerCrop();
            options.placeholder(R.drawable.ic_launcher_background);
            String filePath = mClipInfoArray.get(position).getFilePath();
            Glide.with(mContext)
                    .load(filePath)
                    .apply(options)
                    .into(holder.mImageView);

        }

        @Override
        public int getItemCount() {
            return mClipInfoArray.size();
        }

        @Override
        public void onItemMoved(int fromPosition, int toPosition) {
            mSelectPos = toPosition;
            Collections.swap(mClipInfoArray, fromPosition, toPosition);
            notifyItemMoved(fromPosition, toPosition);
            if (m_onItemClickListener != null)
                m_onItemClickListener.onItemMoved(fromPosition, toPosition);
            Log.e("条目移动", "6666");
        }

        @Override
        public void onItemDismiss(int position) {
            mClipInfoArray.remove(position);
            notifyItemRemoved(position);
            if (m_onItemClickListener != null)
                m_onItemClickListener.onItemDismiss(position);
            Log.e("条目移动", "7777");
        }

        @Override
        public void removeAll() {
            mClipInfoArray.clear();
            if (m_onItemClickListener != null)
                m_onItemClickListener.removeall();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            ImageView mImageView;

            ViewHolder(final View itemView) {
                super(itemView);
                mImageView = (ImageView) itemView.findViewById(R.id.edit_centerimage);
            }
        }
    }

    public interface CallBack {
        public void finshCallBack();
    }
}
